//
//  AESTool.m
//  Objective-cDemo
//
//  Created by ShareAnimation on 2019/8/9.
//  Copyright © 2019 爱丽丝的梦境. All rights reserved.
//

#import "AESTool.h"
#import "YYCategories.h"

static NSString *const kAESIv = @"!yk#dj*15ekdhwix";
static NSString *const kAESKey = @"!yk#dj*1lx;huo1c?97_f2.d3xd1ourd";
@implementation AESTool


/**
 对字符串aes加密
 
 @param sourceString 加密的字符串
 @return 返回base64编码的aes密文
 */
+ (NSString *)aesEncrypt:(NSString *)sourceString {
    NSData *sourceData = [sourceString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSData *ivaData = [kAESIv dataUsingEncoding:NSUTF8StringEncoding];
    NSData *kKeyData = [kAESKey dataUsingEncoding:NSUTF8StringEncoding];
    
    NSData *resultData = [sourceData aes256EncryptWithKey:kKeyData iv:ivaData];
    
    NSString *base64String = [resultData base64EncodedString];
    
    return base64String;
}


/**
 对base64编码的aes进行解密
 
 @param base64String base64编码的密文
 @return 返回原本的字符串
 */
+ (NSString *)aesDecrypt:(NSString *)base64String {
    NSData *ivaData = [kAESIv dataUsingEncoding:NSUTF8StringEncoding];
    NSData *kKeyData = [kAESKey dataUsingEncoding:NSUTF8StringEncoding];
    
    NSData *data = [NSData dataWithBase64EncodedString:base64String];
    NSData *resultData = [data aes256DecryptWithkey:kKeyData iv:ivaData];
    
    NSString *result = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
    
    return result;
}
@end
